package MTG::MagiccardsInfo;
use Moose;
use DBI qw(:sql_types);
my $dbh = DBI->connect("dbi:SQLite:db.sqlite","","");
use Data::Dumper;
use strict;
use utf8;





use base "MTG";

has 'cur_set' => ('is' => 'rw', default => sub { { } });


sub run
{
	my ($self) = @_;
	
	foreach my $set (keys %{$self->config->{'sets'}})
	{	
		printf("Work With %s\n", $set);		
		$self->cur_set($self->config->{'sets'}->{$set});
		$self->parse();
	}
}



sub parse_rus_part
{
	my ($self, $cards) = @_;
	
	if ($self->cur_set->{'dbparse_rus'})
	{
		$self->www->get($self->cur_set->{'dbparse_rus'});
		
		
		$self->clean_tree();
		my $content = $self->www->content();
		$content =~ s#\n##go;
		$content =~ s#<br><br>#. #gio;
		$self->tree->parse($content);
		$self->tree->normalize_content();
		my $root_table  =   ($self->tree->find_by_tag_name('table'))[3];
		my @trs = $root_table->find_by_tag_name('tr');
		
		foreach my $tr (@trs)
		{
			
			my @tds = $tr->find_by_tag_name('td');
	
			if (scalar(@tds) == 1)
			{
				next;
			}	
			
			foreach my $td (@tds)
			{
			
				my $href = ($td->find_by_tag_name('a'))[0]->attr('href');
				my $id = $1 if ($href=~/(\d+)\.html/i);
				
				$cards->{$id}->{'name_rus'} = ($td->find_by_tag_name('span'))[0]->as_text();
				my $type_mana =  ($td->find_by_tag_name('p'))[1]->as_text();
				my @tmp = split(', ', $type_mana);
					
				
				$cards->{$id}->{'rus_type'} = $tmp[0];
				
				
				$cards->{$id}->{'text_rus'} = ($td->find_by_tag_name('p'))[2]->as_text() if (($td->find_by_tag_name('p'))[2]);
				$cards->{$id}->{'quote_rus'} = ($td->find_by_tag_name('p'))[3]->as_text() if (($td->find_by_tag_name('p'))[3]);
				
			}
		}
	}
	
}

sub parse_eng_part
{
	my ($self, $cards) = @_;
	
	$self->www->get($self->cur_set->{'dbparse_eng'});
	
	
	$self->clean_tree();
	my $content = $self->www->content();
	$content =~ s#\n##go;
	$content =~ s#<br><br>#. #gio;
	$self->tree->parse($content);
	$self->tree->normalize_content();
	my $root_table  =   ($self->tree->find_by_tag_name('table'))[3];
	my @trs = $root_table->find_by_tag_name('tr');
	
	foreach my $tr (@trs)
	{
		
		my @tds = $tr->find_by_tag_name('td');

		if (scalar(@tds) == 1 && $tds[0]->attr('colspan') && $tds[0]->attr('colspan') == 4)
		{
			next;
		}	
		
		foreach my $td (@tds)
		{
		
			my $card;
			$card->{'set'} = $self->cur_set->{'set'};
			$card->{'code'} = $self->cur_set->{'code'};

			$card->{'name_eng'} = ($td->find_by_tag_name('span'))[0]->as_text();
			
			my $href = ($td->find_by_tag_name('a'))[0]->attr('href');
			$card->{'id'} = $1 if ($href=~/(\d+)\.html/i);
			
			my $type_mana =  ($td->find_by_tag_name('p'))[1]->as_text();
			my @tmp = split(', ', $type_mana);
				
			
			$card->{'eng_type'} = $tmp[0];
			if ($tmp[1])
			{
				($card->{'mana'}, $card->{'mana_count'}) = ($1, $2) if ($tmp[1] =~ /^(.+?)\s*\((\d+)\)\s*$/);
			}
			
			$card->{'text_eng'} = ($td->find_by_tag_name('p'))[2]->as_text() if (($td->find_by_tag_name('p'))[2]);
			$card->{'quote_eng'} = ($td->find_by_tag_name('p'))[3]->as_text() if (($td->find_by_tag_name('p'))[3]);
			$cards->{$card->{'id'}} = $card;
		}
	}
}

sub parse_rare_part
{
	my ($self, $cards) = @_;
	
	if ($self->cur_set->{'dbparse_rare'})
	{
		$self->www->get($self->cur_set->{'dbparse_rare'});
		
		$self->clean_tree();
		$self->tree->parse($self->www->content());
		$self->tree->normalize_content();
		my $root_table  =   ($self->tree->find_by_tag_name('table'))[3];
		my @trs = $root_table->find_by_tag_name('tr');
		
		foreach my $tr (@trs)
		{
			next unless ($tr->attr('class'));
			
			my @tds = $tr->find_by_tag_name('td');
			
			my $id = 0;
			for(my $i = 0; $i < scalar(@tds); $i++)
			{
				if ($i == 0)
				{
					my $href = ($tds[0]->find_by_tag_name('a'))[0]->attr('href');
					$id = $1 if ($href=~/(\d+)\.html/i);	
				}
				else
				{
					my $rare = $tds[3]->as_text();
					my $rare_int = 0;
					if ($rare=~ /^common/i)
					{
						$rare_int = 1;
					}
					elsif($rare =~ /^uncommon/i)
					{
						$rare_int = 2;
					}
					elsif($rare =~ /^rare/i)
					{
						$rare_int = 3;
					}
					else
					{
						$rare_int = 4;
					}
					$cards->{$id}->{'rare'} = $rare;
					$cards->{$id}->{'rare_int'} = $rare_int; 
					last;
				}
			}
		}
	}
}

sub parse
{
	my $self = shift;

	my $cards = {};
	
	$self->parse_eng_part($cards);
	$self->parse_rus_part($cards);
	$self->parse_rare_part($cards);
	
	foreach my $key (keys %$cards)
	{
		my $card = $cards->{$key};
		my $sth = $self->dbh->prepare("INSERT INTO cards VALUES (null,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
	  	$sth->execute(
	  	$self->cur_set->{'code_id'},
	  	$card->{'id'},
	  	$card->{'name_eng'},
	  	$card->{'name_rus'},
	  	$card->{'quote_eng'},
	  	$card->{'quote_rus'},
	  	$card->{'text_eng'},
	  	$card->{'text_rus'},
	  	$card->{'mana'},
	  	$card->{'rare_int'},
	  	$card->{'card_type_int'},
	  	$card->{'card_subtype'},
	  	$card->{'ability'},
	  	0
	  	
	  	);
	}

}


1;